<template>
  <base-breadcrumb :title="$route.meta.title">
    <!-- 双优化未实施情况编辑 -->
    <a-form-model
      ref="ruleForm"
      :model="form"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 18 }"
      :rules="editModel == 0 ? {} : rules"
    >
      <a-spin :spinning="loading">
        <!-- 表格 -->
        <a-card :bordered="false">
          <title-name title="基本信息"></title-name>
          <custom-form :formList="formList" :form="form" :is-emit="false" :editModel="editModel == 0 ? '' : ''">
            <!-- 附件上传 -->
            <template #fileInfoList>
              <template v-if="editModel != 0">
                <l-table businessCode="FACE09" :uploadArrys.sync="form.fileInfoList"></l-table>
              </template>
              <file-link v-else :fileList="form.fileInfoList" style="margin-top: -3px"></file-link>
            </template>
          </custom-form>
        </a-card>
        <!-- 页脚操作栏 -->
        <div class="btn_box page-btn-right-top" v-if="editModel != 0">
          <a-button
            class="button_right"
            type="primary"
            :loading="loadingBtn.SUBMITTED"
            @click="handleSubmit('SUBMITTED')"
            >提交</a-button
          >
          <a-button @click="handleActiveClose">关闭</a-button>
        </div>
        <div class="btn_box page-btn-right-top" v-else>
          <a-button @click="handleActiveClose">关闭</a-button>
        </div>
      </a-spin>
    </a-form-model>
  </base-breadcrumb>
</template>

<script>
import { getOptimizedConditions, updateOptimizedConditions } from '@/api/technical/OptimizedConditions' // api
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import BSelect from '@/components/BComponents/BSelect'
import SelectCompanyAndProject from '@/components/Select/SelectCompanyAndProject'
export default {
  name: 'a' + Date.now(),
  extends: new BaseEditPage(),
  components: {
    BSelect
  },
  data() {
    return {
      loading: false, // 加载控件
      visible: false, //人员选择控件
      editModel: 0, // 操作状态:0 查看;1 编辑;2 新增
      form: { branchComName: '', creatorName: '', createTime: '', viewName: '' }, // 表单
      id: '',
      loadingBtn: {
        DELETE: false,
        DRAFT: false,
        SUBMITTED: false,
        REJECTED: false
      },
      rules: {
        branchComId: [{ required: true, message: '请选择单位名称' }],
        nextMonth: [{ required: true, message: '请选择月份' }],
        content: [{ required: true, message: '请输入计划工作内容', trigger: 'blur' }]
      }, // 校验规则
      init: false // 初始化控件
    }
  },
  async mounted() {
    this.$route.meta.title = ['双优化未实施情况 查看', '双优化未实施情况 编辑', '双优化未实施情况 新增'][this.editModel]
    if (this.editModel != 2) {
      await this.getDetailData()
    } else {
      this.$nextTick(() => {
        // 默认映射联系人为登陆人
        this.form.creatorName = this.$userInfo().name
        this.form.fillId = this.$userInfo().id
        this.form.createTime = this.$dateFormat(new Date(), 'D')
        this.init = true
      })
    }
  },
  computed: {
    formList() {
      return [
        {
          keyName: 'viewName',
          type: 'BaseInput',
          disabled: true,
          label: '所属单位',
          span: 16
        },
        // {
        //   keyName: 'projectName',
        //   type: 'BaseInput',
        //   disabled: true,
        //   label: '所属项目'
        // },

        {
          keyName: 'name',
          type: 'BaseInput',
          disabled: true,
          label: '双优化名称'
        },
        {
          keyName: 'passDate',
          type: 'BaseInput',
          disabled: true,
          label: '立项通过时间'
        },
        {
          keyName: 'content',
          type: 'BaseTextarea',
          label: '未实施原因或实施与立项内容不符原因',
          disabled: this.editModel == 0,
          span: 24,
          autoSize: { minRows: 30, maxRows: 30 },
          attrs: {
            maxLength: 3000
          }
        }
      ]
    }
  },
  methods: {
    validateForm() {
      let flag = false
      this.$refs.ruleForm.validate(e => {
        if (e) {
          flag = true
        }
      })
      return flag
    },
    getDetailData() {
      // 不为添加时请求详情
      if (this.editModel != 2) {
        this.loading = true
        getOptimizedConditions({ id: this.id })
          .then(res => {
            this.loading = false
            this.form = { ...this.form, ...res.data }
            this.form.viewName = this.form.branchComName + '/' + this.form.projectName
            this.$nextTick(() => {
              this.init = true
            })
          })
          .catch(() => {
            this.loading = false
          })
      } else {
        this.$nextTick(() => {
          this.init = true
        })
      }
    },
    /**
     * 提交表单
     */
    handleSubmit(status) {
      if (status == 'SUBMITTED' && !this.validateForm()) {
        return
      }
      this.loading = true
      this.loadingBtn[status] = true
      let form = this.$clone(this.form)
      updateOptimizedConditions(form)
        .then(() => {
          this.loading = false
          this.loadingBtn[status] = false
          this.$message.success(status == 'SUBMITTED' ? '提交成功！' : '保存成功')
          this.$multiTab.close()
        })
        .catch(() => {
          this.loadingBtn[status] = false
          this.loading = false
          this.$message.warning(err.data.message)
        })
    }
  }
}
</script>
<style lang="less" scoped></style>
